package cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework.vo.ProduceWorkPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework.vo.ProduceWorkRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.producework.vo.ProduceWorkSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.produce.producework.ProduceWorkDO;
import cn.iocoder.yudao.module.erp.service.business.produce.producework.ProduceWorkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工作中心")
@RestController
@RequestMapping("/erp/produce-work")
@Validated
public class ProduceWorkController {

    @Resource
    private ProduceWorkService produceWorkService;

    @PostMapping("/create")
    @Operation(summary = "创建工作中心")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:create')")
    public CommonResult<Long> createProduceWork(@Valid @RequestBody ProduceWorkSaveReqVO createReqVO) {
        return success(produceWorkService.createProduceWork(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作中心")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:update')")
    public CommonResult<Boolean> updateProduceWork(@Valid @RequestBody ProduceWorkSaveReqVO updateReqVO) {
        produceWorkService.updateProduceWork(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作中心")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:produce-work:delete')")
    public CommonResult<Boolean> deleteProduceWork(@RequestParam("id") Long id) {
        produceWorkService.deleteProduceWork(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作中心")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:query')")
    public CommonResult<ProduceWorkRespVO> getProduceWork(@RequestParam("id") Long id) {
        ProduceWorkDO produceWork = produceWorkService.getProduceWork(id);
        return success(BeanUtils.toBean(produceWork, ProduceWorkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作中心分页")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:query')")
    public CommonResult<PageResult<ProduceWorkRespVO>> getProduceWorkPage(@Valid ProduceWorkPageReqVO pageReqVO) {
        PageResult<ProduceWorkDO> pageResult = produceWorkService.getProduceWorkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProduceWorkRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取工作中心")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:query')")
    public CommonResult<List<ProduceWorkRespVO>> getProduceWorkList(){
        List<ProduceWorkDO> list = produceWorkService.getProduceWorkList();
        return success(BeanUtils.toBean(list,ProduceWorkRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出工作中心 Excel")
    @PreAuthorize("@ss.hasPermission('erp:produce-work:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceWorkExcel(@Valid ProduceWorkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProduceWorkDO> list = produceWorkService.getProduceWorkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工作中心.xls", "数据", ProduceWorkRespVO.class,
                        BeanUtils.toBean(list, ProduceWorkRespVO.class));
    }

}